<template>
  <div>
    <div class="z-question-container z-container" v-loading="loading">
      <!--                                      左侧序号等内容-->
      <div class="z-main-container">
        <!--                                   题干-->
        <div class="z-content-container z-long-text z-large-text">
          <p><strong>【{{ options.QUESTION_TYPE.map[questionData.type] }}】</strong>{{ questionData.content }}</p>
        </div>
        <!--                                   答题内容-->
        <div class="z-container z-offset">
          <!--    判断题-->

          <el-radio-group v-if="options.QUESTION_TYPE.enums.JudgeQuestion.value===questionData.type" v-model="key"
                          :disabled="!isTraining"
          >
            <el-radio v-for="item in options.JUDGE_QUESTION_RIGHT_KEY.list" :label="item.label">
              {{ item.value }}
            </el-radio>
          </el-radio-group>
          <!--    选择题-多选题-->

          <el-checkbox-group v-model="key"
                             v-if="options.QUESTION_TYPE.enums.ChoiceQuestion.value===questionData.type && isMultipleChoice"
                             :disabled="!isTraining"
          >
            <div v-for="(item, index) in questionData.examQuestionBankOptionList">
              <el-checkbox :label="index.toString()">
                {{ options.Choice_Question_Option.map[index] }}.{{ item.content }}
              </el-checkbox>
            </div>
          </el-checkbox-group>
          <!--          选择题-单选题-->
          <el-radio-group v-model="key" :disabled="!isTraining"
                          v-if="options.QUESTION_TYPE.enums.ChoiceQuestion.value===questionData.type && !isMultipleChoice">
            <el-radio v-for="(item, index) in questionData.examQuestionBankOptionList" :label="index.toString()">
              {{ options.Choice_Question_Option.map[index] }}.{{ item.content }}
            </el-radio>
          </el-radio-group>
          <!--    填空题-->
          <div v-if="options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value===questionData.type">
            <el-input v-for="(item, index) in questionData.examQuestionBankOptionList" type="text" v-model="key[index]"
                      show-word-limit
                      :maxlength="255" style="margin-top: 10px" :disabled="!isTraining">
              <template v-slot:prepend>
                <span>{{ index + 1 }}.</span>
              </template>
            </el-input>
          </div>
          <!--      论述题-->

          <el-input v-if="options.QUESTION_TYPE.enums.EssayQuestion.value===questionData.type" type="textarea"
                    :autosize="{ minRows: 2, maxRows: 12}" v-model="key" show-word-limit :maxlength="255"
                    :disabled="!isTraining"></el-input>
        </div>


        <!--                                        得分以及正确错误-->
        <div class="z-container z-offset" v-if="!isTraining || showAnswer">
          <!--        <span>实际得分：</span>-->
          <!--        <el-input-number disabled :controls="false" v-model="gainGrade" :precision="1"/>-->
          <span style="margin-left: 20px">答卷结果：</span>
          <el-switch v-model="result" active-color="#13ce66"
                     inactive-color="#ff4949"
                     :active-value="options.RESULT_TYPE.enums.RIGHT.value"
                     :inactive-value="options.RESULT_TYPE.enums.ERROR.value"
                     :active-text="options.RESULT_TYPE.enums.RIGHT.label"
                     :inactive-text="options.RESULT_TYPE.enums.ERROR.label"
                     disabled
          />
        </div>
        <!--                                        答案-->
        <div class="z-container z-offset" v-if="!isTraining || showAnswer">
          <div style="display: inline-block;width: 50% ;vertical-align: top">
            <span style="display: block">您的答案：</span>
            <p
              :class="[result === options.RESULT_TYPE.enums.RIGHT.value ? 'z-right-key':'z-wrong-key', 'z-long-text']">
              {{ resultContentStr }}
            </p>
          </div>
          <div style="display: inline-block;width: 50%;vertical-align: top">
            <span style="display: block">参考答案：</span>
            <!--          判断题-->
            <p class="z-right-key z-long-text">{{
                rightKeyStr
              }}</p>
          </div>
        </div>
        <!--                                        解析-->
        <div class="z-analysis-container z-container z-offset" v-if="!isTraining">
          <i class='el-icon-s-order z-long-text'
             style="flex-basis: 60px;color: rgb(96, 98, 102);flex-shrink: 0;flex-grow: 0;  flex-wrap: wrap;">解析:</i>
          <span class="z-long-text">{{ questionData.analysis }}</span>
        </div>


      </div>

      <!--    <span class="z-grade">({{ questionData.grade }}分)</span>-->
    </div>
    <div class="z-container z-offset" style="text-align: center">
      <el-button v-if="!isTraining" icon="el-icon-close" @click="back">返回</el-button>
      <el-button v-if="isTraining" icon="el-icon-close" @click="finish">结束训练</el-button>
      <el-button v-if="isTraining" icon='el-icon-s-opportunity' type='success' @click="handleShowAnswer">答案及解析
      </el-button>
      <el-button v-if="isTraining" icon='el-icon-right' type='success' @click="nextQuestion"
                 :disabled="Boolean(disabledDeadLine)">
        下一题
      </el-button>

    </div>
  </div>
</template>

<script>
import {
  Choice_Question_Option,
  JUDGE_QUESTION_RIGHT_KEY,
  QUESTION_TYPE,
  RESULT_TYPE,
  TRAIN_TEST_PAPER_STATE
} from '@/views/train/Enums.js'
import {SEPARATOR} from '@/utils/Constant'
import {answerSpecialQuestion, getSpecialQuestion} from "@/api/train/base/Train";

export default {
  name: "question",
  props: {
    isTraining: {
      required: true,
      type: Boolean,
    },
    trainInfo: {
      required: true,
      type: Object,
      validate(dataToCheck) {
        return dataToCheck.postId && dataToCheck.subjectId && dataToCheck.knowledgePointId
      }
    }
  },
  watch: {},
  data() {
    return {
      loading: false,
      // [下一题]的间隔
      nextQuestionDisabledSecond: 1,
      disabledDeadLine: 1,
      showAnswer: false,
      questionData: {},
      // 这是用于回显填写答案和正确答案的
      resultContentStr: '',
      rightKeyStr: '',
      // 这是用于判卷的
      // gainGrade: 0,
      result: '',
      // 填写的答案
      key: '',
      options: {
        TRAIN_TEST_PAPER_STATE: TRAIN_TEST_PAPER_STATE,
        QUESTION_TYPE: QUESTION_TYPE,
        JUDGE_QUESTION_RIGHT_KEY: JUDGE_QUESTION_RIGHT_KEY,
        Choice_Question_Option: Choice_Question_Option,
        RESULT_TYPE: RESULT_TYPE
      },
      // 是否是多选题
      isMultipleChoice: false,
    }
  },
  methods: {
    handleShowAnswer() {
      this.judge(true)
      this.showAnswer = true
    },
    // 下一题
    nextQuestion() {
      this.showAnswer = false
      this.submit()
      this.questionFetch()
    },
    finish() {
      this.showAnswer = false
      this.submit()
      this.back()
    },
    // 查看题目
    questionView(questionData) {
      this.questionData = questionData
      this.judge(true)
    },
    // 获取题目
    questionFetch() {
      this.loading = true
      getSpecialQuestion(this.trainInfo).then(e => {
        this.questionData = e.data
        this.dataDeal()
        this.disabledDeadLine = this.nextQuestionDisabledSecond
        let deadLine = setInterval(_ => {
          this.disabledDeadLine--
          if (!this.disabledDeadLine) {
            clearInterval(deadLine)
          }
        }, 1000)
      }).finally(_ => {
        this.loading = false
      });
    },
    // 判卷
    // isReview 用于判断是否key是否需要重置
    judge(isReview) {
      switch (this.questionData.type) {
        case this.options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value:
          if (isReview) {
            if (this.questionData.resultContent != undefined &&
              this.questionData.resultContent != undefined &&
              this.questionData.resultContent.includes(SEPARATOR)) {
              this.key = (this.questionData.resultContent.split(SEPARATOR))
            } else {
              this.key = this.questionData.resultContent
            }
          }
          // 如果是填空题,那么需要逐个选项判断,每个项的分数为总分/题目数量(四舍五入),如果全对,那么获得满分;如果不是全对,那么获得每个题的分数*做对的数量的分数
          // 用于判断是否全对
          let isAllRight = this.key.length === this.questionData.examQuestionBankOptionList.length
          this.key.forEach((e, index) => {
            if (e === this.questionData.examQuestionBankOptionList[index].content) {
            } else {
              isAllRight = false
            }
          })
          this.resultContentStr = this.key.join('\t')
          this.rightKeyStr = this.questionData.examQuestionBankOptionList.map(e => e.content).join('\t')
          this.result = isAllRight ? this.options.RESULT_TYPE.enums.RIGHT.value : this.options.RESULT_TYPE.enums.ERROR.value
          break;
        case this.options.QUESTION_TYPE.enums.ChoiceQuestion.value:
          let rightKeys = this.questionData.rightKey.split(',')
          if (rightKeys.length > 1) {
            this.isMultipleChoice = true
          }
          if (this.isMultipleChoice) {
            if (isReview) {
              this.key = this.questionData.resultContent.split(',')
            }
            let rightKeys = this.questionData.rightKey.split(',')
            if (rightKeys.length === this.key.length) {
              let isRight = true
              rightKeys.forEach(e => {
                if (!this.key.includes(e)) {
                  isRight = false
                }
              })
              if (isRight) {
                this.result = this.options.RESULT_TYPE.enums.RIGHT.value
              } else {
                this.result = this.options.RESULT_TYPE.enums.ERROR.value
              }
            } else {
              this.result = this.options.RESULT_TYPE.enums.ERROR.value
            }
            this.resultContentStr = this.key.sort().map(e => this.options.Choice_Question_Option.map[e]).toString()
            this.rightKeyStr = this.questionData.rightKey.split(',').sort().map(e => this.options.Choice_Question_Option.map[e]).toString()
          } else {
            if (isReview) {
              this.key = this.options.Choice_Question_Option.map[this.questionData.resultContent]
            }
            if (this.key === this.questionData.rightKey) {
              this.result = this.options.RESULT_TYPE.enums.RIGHT.value
            } else {
              this.result = this.options.RESULT_TYPE.enums.ERROR.value
            }
            this.resultContentStr = this.options.Choice_Question_Option.map[this.key]
            this.rightKeyStr = this.options.Choice_Question_Option.map[this.questionData.rightKey]
          }
          break;
        case this.options.QUESTION_TYPE.enums.JudgeQuestion.value:
          if (isReview) {
            this.key = this.options.JUDGE_QUESTION_RIGHT_KEY.map[this.questionData.resultContent]
          }
          if (this.key === this.questionData.rightKey) {
            this.result = this.options.RESULT_TYPE.enums.RIGHT.value
          } else {
            this.result = this.options.RESULT_TYPE.enums.ERROR.value
            // this.gainGrade = 0
          }
          this.resultContentStr = this.options.JUDGE_QUESTION_RIGHT_KEY.map[this.key]
          this.rightKeyStr = this.options.JUDGE_QUESTION_RIGHT_KEY.map[this.questionData.rightKey]
          break;
        case this.options.QUESTION_TYPE.enums.EssayQuestion.value:
          if (isReview) {
            this.key = this.questionData.resultContent
          }
          // this.gainGrade = this.questionData.grade
          this.result = this.options.RESULT_TYPE.enums.RIGHT.value
          this.resultContentStr = this.key
          this.rightKeyStr = this.questionData.rightKey
          break;
      }
    },
    // 数据处理
    dataDeal() {
      if (this.questionData.type === this.options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value) {
        this.key = Array(this.questionData.examQuestionBankOptionList.length).fill('')
      } else if (this.questionData.type === this.options.QUESTION_TYPE.enums.ChoiceQuestion.value) {
        let rightKeys = this.questionData.rightKey.split(',')
        if (rightKeys.length > 1) {
          this.isMultipleChoice = true
        }
        // 多选题
        if (this.isMultipleChoice) {
          this.key = []
        } else {
          this.key = ''
        }
      } else {
        this.key = ''
      }
    },
    submit() {
      this.judge(false)
      return answerSpecialQuestion({
        id: this.questionData.recordId,
        questionId: this.questionData.questionId,
        result: this.result,
        content: this.questionData.type === this.options.QUESTION_TYPE.enums.FillBlankQuestionExcel.value ? this.key.join(SEPARATOR) : String(this.key)
      })
    },
    back() {
      this.$emit('back')
    }
  },
  mounted() {
  }
}
</script>

<style lang="scss" scoped>
.z-question-container {
  height: 80vh;
  overflow: auto;
  display: flex;
  flex-direction: row;
  font-family: Microsoft YaHei;
  speak: none;
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  vertical-align: baseline;
  -webkit-font-smoothing: antialiased;
  word-break: break-all;

  p {
    text-indent: 4ch;
    margin: 0;
  }

  .z-statistics-container {
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: 300px;
    flex-wrap: wrap;

  }

  .z-main-container {
    flex-grow: 1;
    flex-shrink: 0;
    padding-right: 20px;
    overflow: hidden;
  }


  .z-right-key {
    color: #13ce66
  }

  .z-wrong-key {
    color: #FF0000;
  }

  .z-analysis-container {
    /*border: 2px solid rgb(96, 98, 102);*/
    min-height: 50px;
    display: flex;
    flex-direction: row;
  }

  .z-container {
    padding-top: 15px;
  }

  .z-content-container {
    display: inline-block;
    /*border: 1px solid #dcdfe6;*/
    /*background-color: #eeeef0;*/
    box-sizing: border-box;

  }

  .z-content {
    display: inline-block;
    padding-right: 40px;
    flex-grow: 1;
    white-space: pre-line;
    border: 1px solid black;
    box-sizing: border-box;
  }

  .z-grade {
    flex-grow: 0;
    flex-shrink: 0;
    flex-wrap: wrap;
    flex-basis: 60px;
    display: inline-block;
  }

  .el-radio {
    display: block;

    ::v-deep .el-radio__label {
      font-size: 16px !important;
    }

    ::v-deep .el-radio__inner {
      height: 16px !important;
      width: 16px !important;
    }
  }

  .el-radio + .el-radio {
    margin-top: 10px;
  }

  .z-long-text {
    line-height: 35px;
  }

  .z-large-text {
    font-size: 18px;
  }

  .z-offset {
    margin-left: 80px;
    margin-right: 40px;
  }
}


</style>
